#encoding:utf-8
import os
import Tkinter
import subprocess
import shutil 
import pyodbc
VS2008=r'D:\vs\Microsoft Visual Studio 9.0\Common7\IDE\devenv.com'

class Application(Tkinter.Frame):
    def bug(self):  
        path=[]    
        path.append(self.editPath.get()+ r'\PACS\lwpacs_all.sln') 
        path.append(self.editPath.get()+ r'\PACS\lwpacs_all.sln')         
        for ipath in path:
            cmd = VS2008+r"  /build debug %s" % ipath
            returnCode = subprocess.Popen(cmd)
            returnCode.wait()
        print 'PACS debug OVER'
        
    def bug64(self):  
        path=[]
        path.append(self.editPath.get()+ r'\PACS\ImageMath\ImageMath.vcproj')  
        path.append(self.editPath.get()+ r'\PACS\PacsUtils\PacsUtils.vcproj') 
        path.append(self.editPath.get()+ r'\PACS\I3DProcess\I3DProcess.vcproj')  
        #path.append(self.editPath.get()+ r'\PACS\lwpacs_all.sln') 
        #path.append(self.editPath.get()+ r'\PACS\lwpacs_all.sln')         
        for ipath in path:
            cmd = VS2008+r"  /build debug|x64 %s" % ipath
            returnCode = subprocess.Popen(cmd)
            returnCode.wait()
        print 'PACS debug OVER'   
        
    def release(self):
        start_directory = r'D:\testdir'
        os.system(VS2008+r" /build debug %s" % start_directory)
       
    def openfile(self):
        os.startfile(self.editPath.get())
        
    def openimage(self):
        os.startfile(r'G:\DicomData')
        
    def openSCU(self):
        startpath=self.editPath.get()+r'\bin\base\TestDicomSCU.exe'
        returnCode = subprocess.Popen(startpath)
        
    def deleteimage(self):
        shutil.rmtree(r'G:\Images')
        connection = pyodbc.connect('DRIVER={SQL Server};SERVER=localhost;DATABASE=PACS70D;UID=sa;PWD=sa')
        cur=connection.cursor()
        cur.execute("delete T_PPatient")
        print cur.rowcount, 'T_PPatient deleted'
        cur.execute("delete T_PStudy")
        print cur.rowcount, 'T_PStudy deleted'
        cur.execute("delete T_NOFITSTUDY ")
        print cur.rowcount, 'T_NOFITSTUDY deleted'
        connection.commit()
        print 'game over'

    def delete3Dimage(self):
        shutil.rmtree(r'G:\Images')
        
    def PACSBugButton(self):
        self.BugButon = Tkinter.Button(self)
        self.BugButon["text"] = "PACSBUG"
        self.BugButon["command"] = self.bug
        self.BugButon.pack({'fill':'both'})
        
    def PACS64BugButton(self):
        self.BugButon = Tkinter.Button(self)
        self.BugButon["text"] = "PACS64BUG"
        self.BugButon["command"] = self.bug64
        self.BugButon.pack({'fill':'both'})
      
    def SCUButton(self):
        self.pathButon = Tkinter.Button(self)
        self.pathButon["text"] = "SCU"
        self.pathButon["command"] = self.openSCU
        self.pathButon.pack({'fill':'both'})
    
    def openfileButton(self):
        self.pathButon = Tkinter.Button(self)
        self.pathButon["text"] = "openfile"
        self.pathButon["command"] = self.openfile
        self.pathButon.pack({'fill':'both'})
        
    def openimageButton(self):
        self.pathButon = Tkinter.Button(self)
        self.pathButon["text"] = "openimage"
        self.pathButon["command"] = self.openimage
        self.pathButon.pack({'fill':'both'})

    def deleteImageButton(self):
        self.pathButon = Tkinter.Button(self)
        self.pathButon["text"] = "deleteimage"
        self.pathButon["command"] = self.deleteimage
        self.pathButon.pack({'fill':'both'})
        
    def delete3DImgeButton(self):
        self.pathButon = Tkinter.Button(self)
        self.pathButon["text"] = "delete3Dimage"
        self.pathButon["command"] = self.delete3Dimage
        self.pathButon.pack({'fill':'both'})         
        
    def UIBugButton(self):
        self.BugButon = Tkinter.Button(self)
        self.BugButon["text"] = "UIBUG"
        self.BugButon["command"] = self.bug
        self.BugButon.pack({'fill':'both'})
     
    def PACSReleaseButton(self):
        self.ReleaseButton = Tkinter.Button(self)
        self.ReleaseButton["text"] = "PACSRelease"
        self.ReleaseButton["command"] = self.release
        self.ReleaseButton.pack({'fill':'both'}) 
        
    def editPath(self):
        default_value = Tkinter.StringVar()
        default_value.set('G:\MySource\med70CCC')
        self.editPath = Tkinter.Entry(self)
        self.editPath["textvariable"] = default_value
        self.editPath["borderwidth"] = 3
        self.editPath["width"] = 30
        self.editPath.pack({'fill':'both'}) 
          
    def quitButton(self):
        self.QUIT = Tkinter.Button(self)
        self.QUIT["text"] = "QUIT"
        self.QUIT["fg"]   = "red"
        self.QUIT["command"] =  self.quit
        self.QUIT.pack({'fill':'both'})
        
    def createWidgets(self):
        self.openfileButton()
        self.SCUButton()
        self.openimageButton()
        self.deleteImageButton()
        self.delete3DImgeButton()
        self.PACSBugButton()
        self.PACS64BugButton()
        self.UIBugButton()
        self.PACSReleaseButton()     
        self.editPath()
        self.quitButton()
        
       
    def __init__(self, master=None):
        Tkinter.Frame.__init__(self, master)
        self.pack()
        self.createWidgets()

root = Tkinter.Tk()
app = Application(master=root)
app.master.title("My Application")
app.mainloop()
root.destroy()
